package com.shinhye.elephantsafe;

import java.io.File;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Toast;

import com.shinhye.elephantsafe.utils.SmsBackupUtils;
import com.shinhye.elephantsafe.utils.SmsBackupUtils.SmsProgress;

public class AdvanceToolActivity extends Activity {
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(android.os.Message msg) {
			switch(msg.what) {
			case 0x00:
				Toast.makeText(AdvanceToolActivity.this, (String) msg.obj, Toast.LENGTH_SHORT).show();
				break;
			}
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_advancetool);
	}
	
	public void phonebelog(View v) {
		Intent intent = new Intent(this, NumberBelongActivity.class);
		startActivity(intent);
	}
	
	public void smsbackup(View v) {
		final ProgressDialog dialog = new ProgressDialog(this);
		dialog.setTitle("短信备份");
		dialog.setIcon(android.R.drawable.ic_input_get);
		dialog.setMessage("正在备份中，请稍候...");
		dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		
		if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
				dialog.show();
				final File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/ElephantSafe/Backup");
				dir.mkdirs();
				new Thread(new Runnable() {
					@Override
					public void run() {
						try{
							SmsBackupUtils.backupSms(AdvanceToolActivity.this, dir.getAbsolutePath() + "/SmsBackup.xml", new SmsProgress() {
								private int total;
								@Override
								public void inBackup(int progress) {
									dialog.setProgress(progress);
									if(progress == total) {
										Message msg = handler.obtainMessage();
										msg.obj = "短信备份完了！";
										msg.what = 0x00;
										handler.sendMessage(msg);
									}
								}
								@Override
								public void beforeBackup(int total) {
									this.total = total;
									dialog.setMax(total);
								}
							});
						} catch (Exception e) {
							e.printStackTrace();
							Message msg = handler.obtainMessage();
							msg.obj = "短信备份出错了！";
							msg.what = 0x00;
							handler.sendMessage(msg);
						} finally {
							dialog.dismiss();
						}
					}
				}).start();
		} else {
			Toast.makeText(this, "SD卡目前不可用哦！", Toast.LENGTH_SHORT).show();
		}
	}
	
	public void commonnumberquery(View v) {
		Intent intent = new Intent(this, CommonNumberQueryActivity.class);
		startActivity(intent);
	}
	
	public void applock(View v) {
		Intent intent = new Intent(this, AppLockActivity.class);
		startActivity(intent);
	}
	
}
